package thread.synchronize;

import thread.Customer;
import thread.Ticket;

public class SynchronizedTicket extends Ticket {
    private static int count;

    public static Ticket sellOne(Customer buyer) {
        if (count > 0) {
            synchronized (SynchronizedTicket.class) {
                count -= 1;
            }
            Ticket ticket = new SynchronizedTicket(buyer);
            System.out.println("售票处已卖出一张票，购买此票的顾客是" + ticket.getBuyer().getName());
            return ticket;
        }
        return null;
    }

    public synchronized static int getCount() {
        return count;
    }
    
    public static void addCount(int extraCount) {
        count += extraCount > 0 ? extraCount : 0;
    }

    private SynchronizedTicket(Customer buyer) {
        super(buyer);
    }
}
